<?php
/**
* Copyright (C)2001-2099 Zeai.cn All rights reserved.
* E-Mail：supdes#qq.com　QQ:797311
* http://www.zeai.cn
* http://www.yzlove.com
* Last update 2020/03/28 by supdes
*/
require_once '../../../../sub/init.php';
require_once ZEAI.'cache/config_vip.php';
require_once ZEAI.'cache/config_pay.php';
require_once ZEAI.'sub/conn.php';
require_once "lib/WxPay.Api.php";
require_once "WxPay.Config.php";

/*		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'亲，参数好像跑路了'));
		$row = $db->ROW(__TBL_SHOP_ORDER__,"orderid","flag=7 AND cid=".$cook_tg_uid." AND id=".$id,"num");
		if ($row){
			$orderid=$row[0];
			$row = $db->ROW(__TBL_PAY__,"id,flag,paymoney","orderid='$orderid'","num");
			if ($row){
				$payid   = $row[0];
				$payflag = $row[1];
				$paymoney= $row[2];
				if($paymoney>0 && $payflag==1){
					$url=HOST."/api/weixin/pay/refund/zeai_refund.php?submitok=tuikuan&payid=".$payid;
					$return_url=HOST."/m4/shop_my_order.php";
					json_exit(array('flag'=>1,'url'=>$url,'return_url'=>$return_url));
				}
			}
		}
*/		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if($submitok=='tuikuan' && ifint($payid)){
	$row = $db->ROW(__TBL_PAY__,"orderid,flag,paymoney,trade_no,title","id=".$payid,"num");
	if ($row){
		$Porderid  = $row[0];
		$Pflag     = $row[1];
		$Ppaymoney = $row[2];
		$transaction_id = $row[3];
		$ptitle    = $row[4];
		if($Pflag!=1)alert('当前支付订单不支持此操作',$return_url);	
	}else{alert('参数异常',$return_url);	}
	$row = $db->ROW(__TBL_SHOP_ORDER__,"id,cid,flag,orderprice,tg_uid","orderid='$Porderid'","num");
	if ($row){
		$Oid         = $row[0];
		$Ocid        = $row[1];
		$Oflag       = $row[2];
		$Oorderprice = $row[3];
		$Otg_uid     = intval($row[4]);
		if($Oflag!=7)alert('当前订单状态不支持此操作',$return_url);	
	}else{alert('参数异常',$return_url);	}
		
	if($Ppaymoney!=$Oorderprice || $Ocid!=$cook_tg_uid){
		alert('非法操作',$return_url);	
	}
	$total_fee  = $Ppaymoney*100;
	$refund_fee = $total_fee;
	$input = new WxPayRefund();
	$input->SetTransaction_id($transaction_id);
	$input->SetTotal_fee($total_fee);
	$input->SetRefund_fee($refund_fee);
	$config = new WxPayConfig();
	$input->SetOut_refund_no("sdkphp".date("YmdHis"));
	$input->SetOp_user_id($config->GetMerchantId());
	$ret=WxPayApi::refund($config, $input);
	if($ret['result_code']=='SUCCESS' && $ret['return_code']=='SUCCESS'){
		$db->query("UPDATE ".__TBL_SHOP_ORDER__." SET flag=8,tuikuanendtime=".ADDTIME." WHERE id=".$Oid);
		$db->query("UPDATE ".__TBL_PAY__." SET flag=-2 WHERE id=".$payid);
		/**通知买家**/
		//获取买家信息
		$row    = $db->ROW(__TBL_TG_USER__,"openid,subscribe","id=".$Otg_uid,"num");
		$openid = $row[0];$subscribe = $row[1];
		//站内
		$C = '退款成功（资金￥'.$Ppaymoney.'已原路返回或入账到微信钱包）->【'.$ptitle.'】，请注意查收';
		$db->SendTip($Otg_uid,'【'.$ptitle.'】退款成功',$C,'shop');
		//微信
		if (!empty($openid) && $subscribe==1){
			$keyword1 = urlencode($C);
			$keyword3 = urlencode($_ZEAI['siteName']);
			$url      = urlencode($return_url.'?f=8&ifadm=1');
			@wx_mb_sent('mbbh=ZEAI_ADMIN_INFO&openid='.$openid.'&keyword1='.$keyword1.'&keyword3='.$keyword3.'&time='.ADDTIME.'&first='.$first.'&remark='.$remark.'&url='.$url);
		}
		alert('退款成功',$return_url.'?ifadm=1');
	}else{
		alert('退款操作失败，请联系管理员检查支付参数配置问题',$return_url.'?f=7&ifadm=1');	
	}
}?>